﻿using UnityEngine;
using System.Collections;

public class Weapon : MonoBehaviour
{
    public float fireRate;
    public AudioClip[] weaponSounds;

    // Components
    public Transform spawn;
    private LineRenderer laserTracer;
    private LineRenderer rocketTracer;

    // System
    private float secondsBetweenLaserShots;
    private float secondsBetweenRocketShots;
    private float secondsBetweenGrenades;
    private float nextPossibleShot;
    public GameObject Grenade;
    public GameObject Rocket;
    public System.Random rnd = new System.Random (1023);


    //Weapon specifics
    public float grenadeVelocity;
    public float rocketVelocity;
    

    void Start ()
    {
        secondsBetweenLaserShots = 60 / fireRate;
        
        if (GetComponent<LineRenderer> ()) {
            laserTracer = GetComponent<LineRenderer> ();
        }
        
        secondsBetweenRocketShots = 60 / fireRate;
        
        if (GetComponent<LineRenderer> ()) {
            rocketTracer = GetComponent<LineRenderer> ();
        }

        secondsBetweenGrenades = 75 / fireRate;
        
        if (GetComponent<LineRenderer> ()) {
            rocketTracer = GetComponent<LineRenderer> ();
        }
    }

    public void ShootLaser ()
    {
        if (CanShoot () == true) {
            Ray ray = new Ray (spawn.position, spawn.forward);
            RaycastHit hit;
            
            float shotDistance = 100;
            
            if (Physics.Raycast (ray, out hit, shotDistance)) {
                shotDistance = hit.distance;
            }
            
            nextPossibleShot = Time.time + secondsBetweenLaserShots;
            
            AudioSource.PlayClipAtPoint (weaponSounds [0], transform.position, 0.6f);
            
            if (laserTracer) {
                StartCoroutine ("RenderLaserTracer", ray.direction * shotDistance);
            }
        }
    }

    public void ThrowGrenade ()
    {                
        if (CanShoot ()) {
            GameObject theGrenade = (GameObject)Instantiate (Grenade, spawn.position + spawn.forward * 2, spawn.rotation);
            theGrenade.rigidbody.transform.Rotate ((rnd.Next (255)), (rnd.Next (128)), (rnd.Next (315)));
            theGrenade.rigidbody.AddForce (spawn.transform.forward * grenadeVelocity * 2, ForceMode.Impulse);
            
            
            nextPossibleShot = Time.time + secondsBetweenGrenades;
            //AudioSource.PlayClipAtPoint(weaponSounds[2], transform.position);
            //todo: Make self not collide with grenade                        
        }    
    }

    public void ShootRocket ()
    {
        if (CanShoot () == true) {
            Ray ray = new Ray (spawn.position, spawn.forward);
            RaycastHit hit;
            
            float shotDistance = 25;
            
            if (Physics.Raycast (ray, out hit, shotDistance)) {
                shotDistance = hit.distance;
            }
            GameObject theRocket = (GameObject)Instantiate (Rocket, spawn.position + spawn.forward * 2, spawn.rotation);
            theRocket.transform.Rotate (90, 0, 0);
            theRocket.rigidbody.AddForce (spawn.transform.forward * rocketVelocity, ForceMode.Impulse);
            
            AudioSource.PlayClipAtPoint (weaponSounds [1], transform.position);
            
            if (rocketTracer) {
                StartCoroutine ("RenderRocketTracer", ray.direction * shotDistance);
            }
        }
    }

    private bool CanShoot ()
    {
        bool canShoot = true;

        if (Time.time < nextPossibleShot) {
            canShoot = false;
        }

        return canShoot;
    }

    IEnumerator RenderLaserTracer (Vector3 hitPoint)
    {
        laserTracer.enabled = true;
        laserTracer.SetPosition (0, spawn.position);
        laserTracer.SetPosition (1, spawn.position + hitPoint);
        yield return new WaitForSeconds (0.0165f);
        laserTracer.enabled = false;
    }
    
    IEnumerator RenderRocketTracer (Vector3 hitPoint)
    {
        rocketTracer.enabled = true;
        rocketTracer.SetPosition (0, spawn.position);
        rocketTracer.SetPosition (1, spawn.position + hitPoint);
        yield return new WaitForSeconds (0.03f);
        rocketTracer.enabled = false;
    }
}
